package org.jeecg.modules.exTenderFileClariModi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.exProject.constant.ExProjectStepEnum;
import org.jeecg.modules.exProject.entity.ExProject;
import org.jeecg.modules.exProject.service.IExProjectService;
import org.jeecg.modules.exTenderFileClariModi.entity.ExTenderFileClariModi;
import org.jeecg.modules.exTenderFileClariModi.mapper.ExTenderFileClariModiMapper;
import org.jeecg.modules.exTenderFileClariModi.service.IExTenderFileClariModiService;
import org.jeecg.modules.exTenderFileSetVariable.entity.ExTenderFileSetVariable;
import org.jeecg.modules.exTenderFileSetVariable.service.IExTenderFileSetVariableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description: 招标文件
 * @Author: jeecg-boot
 * @Date:   2024-09-11
 * @Version: V1.0
 */
@Service
public class ExTenderFileClariModiServiceImpl extends ServiceImpl<ExTenderFileClariModiMapper, ExTenderFileClariModi> implements IExTenderFileClariModiService {
    @Autowired
    private IExProjectService iExProjectService;
    @Autowired
    private IExTenderFileSetVariableService iExTenderFileSetVariableService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveTenderFile(ExTenderFileClariModi exTenderFileClariModi) {
        // 如果已有不允许新增
        ExTenderFileClariModi old = this.getOne(new LambdaQueryWrapper<ExTenderFileClariModi>().eq(ExTenderFileClariModi :: getSectionId,exTenderFileClariModi.getSectionId()));
        if(old != null){
            throw new JeecgBootException("该标段已创建招标文件，请选择其他标段");
        }

        this.save(exTenderFileClariModi);
        //初始化变量
        List<ExTenderFileSetVariable> exTenderFileSetVariableList = iExTenderFileSetVariableService.getBySectionId("0");
        exTenderFileSetVariableList.forEach(exTenderFileSetVariable -> {
            exTenderFileSetVariable.setId(null);
            exTenderFileSetVariable.setProjectId(exTenderFileClariModi.getProjectId());
            exTenderFileSetVariable.setSectionId(exTenderFileClariModi.getSectionId());
            exTenderFileSetVariable.setCreateTime(new Date());
        });
        iExTenderFileSetVariableService.saveBatch(exTenderFileSetVariableList);
        // 新建后则更新项目信息的步骤为招标文件
        ExProject exProject = iExProjectService.getById(exTenderFileClariModi.getProjectId());
        if (null != exProject && exProject.getStep() < ExProjectStepEnum.ZBGG.getValue()) {
            exProject.setStep(ExProjectStepEnum.ZBGG.getValue());
            iExProjectService.updateById(exProject);
        }
    }
}
